package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.TransModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 变压器业务
 * @author zhaokf
 * @date 2013-04-19
 */
public class TransService extends BaseService
{
	/**
	 * 分页列出变压器
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<TransModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("tran_no") != null){
				where = " AND tran_no=" + search.get("tran_no");
			}
			if (search.get("tran_name") != null){
				where = " AND tran_name LIKE '%" + search.get("tran_name") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<TransModel> Trans = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM G_TRAN " + where;

		try{
			Trans = TransModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return Trans;
	}



	/**
	 * 增加变压器信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){

//		String no = params.get("tran_code")==null?"":params.get("tran_code").toString();
//		String name = params.get("tran_name")==null?"":params.get("tran_name").toString();
//		String org = params.get("tran_org_no")==null?"":params.get("tran_org_no").toString();
//		String subs = params.get("tran_subs_id")==null?"":params.get("tran_subs_id").toString();
//		String st=params.get("tran_st_id")==null?"":params.get("tran_st_id").toString();
//		String instDate=params.get("tran_instDate")==null?"":params.get("tran_instDate").toString();
//		String runDate=params.get("tran_runDate")==null?"":params.get("tran_runDate").toString();
//		String runNum=params.get("tran_runNum")==null?"":params.get("tran_runNum").toString();
//		String addr=params.get("tran_addr")==null?"":params.get("tran_addr").toString();
//		String lossFlg=params.get("tran_lossFlg")==null?"":params.get("tran_lossFlg").toString();
//		String lossDate=params.get("tran_lossDate")==null?"":params.get("tran_lossDate").toString();
//		String apvalue=params.get("tran_ap_value")==null?"":params.get("tran_ap_value").toString();
//		String rpvalue=params.get("tran_rp_value")==null?"":params.get("tran_rp_value").toString();
//		String msType=params.get("tran_msType")==null?"":params.get("tran_msType").toString();
//		String coolMode=params.get("tran_coolMode")==null?"":params.get("tran_coolMode").toString();
//		String proterctMode=params.get("tran_protectMode")==null?"":params.get("tran_protectMode").toString();
//		String factoryName=params.get("tran_factoryName")==null?"":params.get("tran_factoryName").toString();
//		String factoryNo=params.get("tran_factoryNo")==null?"":params.get("tran_factoryNo").toString();
//		String factoryDate=params.get("tran_factoryDate")==null?"":params.get("tran_factoryDate").toString();
//		String priv=params.get("tran_priv_code")==null?"":params.get("tran_priv_code").toString();
//		String status=params.get("tran_status")==null?"":params.get("tran_status").toString();
//		String remark=params.get("tran_remark")==null?"":params.get("tran_remark").toString();
//		
//		String sql = "select * from g_tran where tran_code=?";
//		TransModel tm = TransModel.dao.findFirst(sql, no);
//		if (tm == null){
//			tm =new TransModel();
//			tm.set("tran_id","G_TRAN_SEQ.nextval");
//			if("".equals(no)){tm.set("tran_code",no);}
//			if("".equals(name)){tm.set("tran_name", name);}
//			if("".equals(org)){tm.set("tran_org_no", org);}
//			if("".equals(subs)){tm.set("tran_subs_id", subs);}
//			if("".equals(st)){tm.set("tran_st_id",st);}
//			if("".equals(instDate)){tm.set("tran_instDate",instDate);}
//			if("".equals(runDate)){tm.set("tran_runDate",runDate);}
//			if("".equals(runNum)){tm.set("tran_runNum",runNum);}
//			if("".equals(addr)){tm.set("tran_addr",addr);}
//			if("".equals(lossFlg)){tm.set("tran_lossFlg",lossFlg);}
//			if("".equals(lossDate)){tm.set("tran_lossDate",lossDate);}
//			if("".equals(apvalue)){tm.set("tran_ap_value",apvalue);}
//			if("".equals(rpvalue)){tm.set("tran_rp_value",rpvalue);}
//			if("".equals(msType)){tm.set("tran_msType",msType);}
//			if("".equals(coolMode)){tm.set("tran_coolMode",coolMode);}
//			if("".equals(proterctMode)){tm.set("tran_protectMode",proterctMode);}
//			if("".equals(factoryName)){tm.set("tran_factoryName",factoryName);}
//			if("".equals(factoryNo)){tm.set("tran_factoryNo",factoryNo);}
//			if("".equals(factoryDate)){tm.set("tran_factoryDate",factoryDate);}
//			if("".equals(priv)){tm.set("tran_priv_code",priv);}
//			if("".equals(status)){tm.set("tran_status",status);}
//			if("".equals(remark)){tm.set("tran_remark",remark);}
//			tm.save();
//			return true;
//		}
		Record record = new Record();
		record.set("tran_id", Db.queryBigDecimal("select g_tran_seq.nextval from dual").intValue());
		record.set("tran_code", params.get("tran_code").toString());
		record.set("tran_name", params.get("tran_name").toString());
		record.set("tran_org_no", params.get("tran_org_no").toString());
		record.set("tran_st_id", params.get("tran_st_id").toString());
		record.set("tran_subs_id", params.get("tran_subs_id").toString());
		boolean isupadded = Db.save("g_tran", "tran_id",record);
		if(isupadded){
			return true;
		}
		return setError("你访问的变压器已存在！");
	}

	/**
	 * 修改变电器信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
//		String no = params.get("tran_code")==null?"":params.get("tran_code").toString();
//		String name = params.get("tran_name")==null?"":params.get("tran_name").toString();
//		String org = params.get("tran_org_no")==null?"":params.get("tran_org_no").toString();
//		String subs = params.get("tran_subs_id")==null?"":params.get("tran_subs_id").toString();
//		String st=params.get("tran_st_id")==null?"":params.get("tran_st_id").toString();
//		String instDate=params.get("tran_instDate")==null?"":params.get("tran_instDate").toString();
//		String runDate=params.get("tran_runDate")==null?"":params.get("tran_runDate").toString();
//		String runNum=params.get("tran_runNum")==null?"":params.get("tran_runNum").toString();
//		String addr=params.get("tran_addr")==null?"":params.get("tran_addr").toString();
//		String lossFlg=params.get("tran_lossFlg")==null?"":params.get("tran_lossFlg").toString();
//		String lossDate=params.get("tran_lossDate")==null?"":params.get("tran_lossDate").toString();
//		String apvalue=params.get("tran_ap_value")==null?"":params.get("tran_ap_value").toString();
//		String rpvalue=params.get("tran_rp_value")==null?"":params.get("tran_rp_value").toString();
//		String msType=params.get("tran_msType")==null?"":params.get("tran_msType").toString();
//		String coolMode=params.get("tran_coolMode")==null?"":params.get("tran_coolMode").toString();
//		String proterctMode=params.get("tran_protectMode")==null?"":params.get("tran_protectMode").toString();
//		String factoryName=params.get("tran_factoryName")==null?"":params.get("tran_factoryName").toString();
//		String factoryNo=params.get("tran_factoryNo")==null?"":params.get("tran_factoryNo").toString();
//		String factoryDate=params.get("tran_factoryDate")==null?"":params.get("tran_factoryDate").toString();
//		String priv=params.get("tran_priv_code")==null?"":params.get("tran_priv_code").toString();
//		String status=params.get("tran_status")==null?"":params.get("tran_status").toString();
//		String remark=params.get("tran_remark")==null?"":params.get("tran_remark").toString();
//		String sql = "select * from g_tran where tran_code=?";
//		TransModel tm = TransModel.dao.findFirst(sql, no);
//		if (tm != null){
//			if("".equals(no)){tm.set("tran_code",no);}
//			if("".equals(name)){tm.set("tran_name", name);}
//			if("".equals(org)){tm.set("tran_org_no", org);}
//			if("".equals(subs)){tm.set("tran_subs_id", subs);}
//			if("".equals(st)){tm.set("tran_st_id",st);}
//			if("".equals(instDate)){tm.set("tran_instDate",instDate);}
//			if("".equals(runDate)){tm.set("tran_runDate",runDate);}
//			if("".equals(runNum)){tm.set("tran_runNum",runNum);}
//			if("".equals(addr)){tm.set("tran_addr",addr);}
//			if("".equals(lossFlg)){tm.set("tran_lossFlg",lossFlg);}
//			if("".equals(lossDate)){tm.set("tran_lossDate",lossDate);}
//			if("".equals(apvalue)){tm.set("tran_ap_value",apvalue);}
//			if("".equals(rpvalue)){tm.set("tran_rp_value",rpvalue);}
//			if("".equals(msType)){tm.set("tran_msType",msType);}
//			if("".equals(coolMode)){tm.set("tran_coolMode",coolMode);}
//			if("".equals(proterctMode)){tm.set("tran_protectMode",proterctMode);}
//			if("".equals(factoryName)){tm.set("tran_factoryName",factoryName);}
//			if("".equals(factoryNo)){tm.set("tran_factoryNo",factoryNo);}
//			if("".equals(factoryDate)){tm.set("tran_factoryDate",factoryDate);}
//			if("".equals(priv)){tm.set("tran_priv_code",priv);}
//			if("".equals(status)){tm.set("tran_status",status);}
//			if("".equals(remark)){tm.set("tran_remark",remark);}
//			tm.update();
//			return true;
//		}
		Record record = new Record();
		record.set("tran_id",  params.get("tran_id").toString());
		record.set("tran_code", params.get("tran_code").toString());
		record.set("tran_name", params.get("tran_name").toString());
		record.set("tran_org_no", params.get("tran_org_no").toString());
		record.set("tran_st_id", params.get("tran_st_id").toString());
		record.set("tran_subs_id", params.get("tran_subs_id").toString());
		boolean isupadded = Db.update("g_tran", "tran_id",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的变压器存在！");
	}

	/**
	 * 删除变压器信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("tran_code")==null?"":params.get("tran_code").toString();
		String sql = "select * from g_tran where tran_code=?";
		TransModel tm = TransModel.dao.findFirst(sql, no);
		if (tm != null)
		{
			tm.delete();
			return true;
		}
		return setError("您访问的变压器不存在！");
	}
}

